package com.hot100;

public class Solution160 {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        int lA = 0;
        int lB = 0;
        ListNode curA = headA;
        while (curA != null) {
            lA++;
            curA = curA.next;
        }
        ListNode curB = headB;
        while (curB != null) {
            lB++;
            curB = curB.next;
        }
        curA = headA;
        curB = headB;
        int tmp;
        if (lA > lB) {
            tmp = lA - lB;
            while (tmp-- > 0) curA = curA.next;
        } else {
            tmp = lB - lA;
            while (tmp-- > 0) curB = curB.next;
        }
        while (curA != null && curB != null) {
            if (curA == curB) return curA;
            curA = curA.next;
            curB = curB.next;
        }
        return null;
    }
}
